/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.satacredito.queue;

import co.edu.unal.satacredito.queue.entity.TransactionPojo;
import com.google.gson.Gson;
import java.net.HttpURLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.jms.QueueSender;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 *
 * @author Oscardu
 */
@Stateless
@Path("/transaction")
public class WSReception {

    @GET
    public String getMethod() {
        return "SATACREDITO TRANSACTION RECEPTION: Use POST to send data.";
    }

    @POST
    @Path("/post")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response postMethod(String traza){
        
        
        System.out.println("WSReception: Received from client");
        
        
        Gson jsonTraza = new Gson();
        TransactionPojo Transaction = jsonTraza.fromJson(traza, TransactionPojo.class);
        QueueWriter qw = new QueueWriter();
        
        try {
            
            QueueSender sender = qw.OpenQueueConnection();
            System.out.println("WSReception: "+Transaction);
            qw.SendToQueue(sender, Transaction);
            qw.CloseQueueConnection();
            
            return Response.status(HttpURLConnection.HTTP_CREATED).build();
        } catch (Exception e) {
               Logger.getLogger(QueueWriter.class.getName()).log(Level.SEVERE, null, e);
               return Response.status(HttpURLConnection.HTTP_CONFLICT).build();
        }

    }
}
